package com.atls.eduservice.controller;


import com.atls.commonutils.R;
import com.atls.eduservice.entity.EduTeacher;
import com.atls.eduservice.entity.vo.TeacherQuery;
import com.atls.eduservice.service.EduTeacherService;
import com.atls.servicebase.exception.GuliException;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 讲师 前端控制器
 * </p>
 *
 * @author ls
 * @since 2021-01-23
 */

@Api("讲师管理")
@RestController
@RequestMapping("/eduservice/teacher")
@CrossOrigin
public class EduTeacherController {

    @Autowired
    private EduTeacherService teacherService;

    //1.查询讲师列表
    //rest风格
    @ApiOperation("所有讲师列表")
    @GetMapping("findAll")
    public R findAllTeacher(){
        List<EduTeacher> list = teacherService.list(null);
        return R.ok().data("items",list);
    }

    //2.根据讲师id删除讲师（逻辑删除）
    @ApiOperation("根据id逻辑删除讲师")
    @DeleteMapping("{id}")
    public R removeTeacherById(
            @ApiParam(name = "id",value = "讲师ID",required = true)
            @PathVariable String id){
        boolean flag = teacherService.removeById(id);
        if(flag){
          return R.ok();
        }else {
            return R.error();
        }
    }




    //3.讲师条件查询
    /**
     * 讲师分页查询
     * @param current 当前页
     * @param limit   每页显示的记录数
     * @return
     */
    @ApiOperation("讲师分页查询")
    @GetMapping("pageTeacher/{current}/{limit}")
    public R pageListTeacher(@ApiParam(name = "current",value = "当前页",required = true) @PathVariable Long current,
                             @ApiParam(name = "limit",value = "每页显示条数",required = true)@PathVariable Long limit){

        //创建page对象
        Page<EduTeacher> pageTeacher=new Page<>(current,limit);

        //实现分页
        teacherService.page(pageTeacher,null);
        //总记录数
        long total = pageTeacher.getTotal();
        //数据List集合
        List<EduTeacher> records = pageTeacher.getRecords();
        return R.ok().data("total",total).data("rows",records);
    }


    //4.讲师条件分页查询
    /**
     * 讲师分页条件查询
     * @param current 当前页
     * @param limit   每页显示记录数
     * @param teacherQuery
     * @return
     */
    @ApiOperation("讲师分页条件查询")
    @PostMapping("pageTeacherCondition/{current}/{limit}")
    public R pageTeacherCondition(@ApiParam(name = "current",value = "当前页",required =true) @PathVariable Long current,
                                  @ApiParam(name = "limit",value = "每页显示记录数",required = true) @PathVariable Long limit,
                                  @RequestBody(required = false) TeacherQuery teacherQuery){

        Page<EduTeacher> pageTeacher=new Page<>(current,limit);
        teacherService.pageQuery(pageTeacher,teacherQuery);
        long total = pageTeacher.getTotal();
        List<EduTeacher> records = pageTeacher.getRecords();
        return R.ok().data("total",total).data("rows",records);
    }


    //5.添加讲师
    @ApiOperation("添加讲师")
    @PostMapping("addTeacher")
    public R addTeacher(@RequestBody EduTeacher eduTeacher){
        boolean flag = teacherService.save(eduTeacher);
        if(flag){
            return R.ok();
        }else{
            return R.error();
        }
    }

    //6.根据讲师ID查询讲师
    @ApiOperation("根据讲师ID查询讲师")
    @GetMapping("getTeacher/{id}")
    public R getTeacher(@ApiParam(name = "id",value = "讲师ID", required = true)
                            @PathVariable String id){
        EduTeacher eduTeacher = teacherService.getById(id);
        return R.ok().data("item",eduTeacher);
    }

    //7.根据讲师ID修改讲师
    @ApiOperation("修改讲师")
    @PostMapping("updateTeacher")
    public R updateTeacher(@RequestBody EduTeacher eduTeacher){
        boolean flag = teacherService.updateById(eduTeacher);
        if(flag){
            return R.ok();
        }else {
            return R.error();
        }
    }
}

